from collections import deque

if __name__ == '__main__':
    # 创建列表
    my_list = [1, "apple", True, 3.14]  # 支持混合类型

    # 添加元素
    my_list.append("new_item")          # 末尾追加
    print(my_list) # 输出：[1, 'apple', True, 3.14, 'new_item']
    my_list.insert(1, "inserted_item")  # 指定位置插入
    print(my_list) # 输出：[1, 'inserted_item', 'apple', True, 3.14, 'new_item']

    # 删除元素
    my_list.pop(2)                      # 删除索引2的元素（返回被删除值）
    print(my_list) # 输出：[1, 'inserted_item', True, 3.14, 'new_item']
    my_list.remove(3.14)             # 删除第一个匹配的元素
    print(my_list) # 输出：[1, 'inserted_item', True, 'new_item']

    # 切片操作
    sub_list = my_list[1:3]             # 截取索引1到2的元素
    print(sub_list) # 输出：['inserted_item', True]

    # 列表推导式
    squares = [x**2 for x in range(5)]
    print(squares) # 输出：[0, 1, 4, 9, 16]

    squares = [x ** 2 for x in range(5)]
    print(squares)

    even_squares = [x ** 2 for x in range(10) if x % 2 == 0]
    print(even_squares)

    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    flattened = [num for row in matrix for num in row]
    print(flattened)

    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    filtered = [num for row in matrix for num in row if num > 5]
    print(filtered)

    #=============元组

    my_tuple = (1, [2, 3], "hello")  # 包含可变列表

    # my_tuple[0] = 10  # 报错：TypeError

    my_tuple[1].append(4)  # 合法操作
    print(my_tuple)  # 输出：(1, [2, 3, 4], 'hello')


    #=============字典
    my_dict = {"name": "Alice", "age": 11, "city": "B"}
    my_dict["city"] = "Beijing"  # 新增键值对
    my_dict["age"] = 26
    del my_dict["name"]
    print(my_dict) # 输出: {'age': 26, 'city': 'Beijing'}

    for key, value in my_dict.items():
        print(f"{key}: {value}") #  age:26 city:Beijing

    my_set = {1, 2, 3, 2}  # 自动去重，结果为{1,2,3}
    print(my_set) # 输出: {1,2,3}

    # 添加/删除元素
    my_set.add(4)  # 添加元素
    my_set.remove(2)  # 删除元素（若不存在会报错）
    print(my_set) # 输出: {1,3,4}

    # 集合运算
    set_a = {1, 2, 3}
    set_b = {3, 4, 5}
    print(set_a & set_b)  # 交集：{3}
    print(set_a | set_b)  # 并集：{1,2,3,4,5}

    queue = deque(["a", "b"])
    queue.append("c")  # 入队
    front = queue.popleft()
    print(front) # 输出 a

    print(queue) # 输出 deque(['b', 'c'])


